package com.wawei.seeds.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.wawei.seeds.R;
import com.wawei.seeds.me.MeServiceDetailActivity;
import com.wawei.seeds.services.WalnutMyServiceServiceHandler;
import com.wawei.seeds.utils.RetrofitNetworkService;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 *
 * Created by t27891 on 15/6/28.
 */
public class WalnutServiceDetailItemActivityAdapter extends SeedsBaseAdapter<WalnutMainActivityAdapter.WalnutEventAppointmentMan> {
    private MeServiceDetailActivity.ServiceDetailNotifyHandler notifyHandler;

    public WalnutServiceDetailItemActivityAdapter(Context context) {
        super(context);

        notifyHandler = new MeServiceDetailActivity.ServiceDetailNotifyHandler((MeServiceDetailActivity)context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        ViewHolder viewHolder;
        if(null == view){
            view = LayoutInflater.from(context).inflate(R.layout.fragment_walnut_me_service_detail_item, parent, false);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        }
        else {
            viewHolder = (ViewHolder)view.getTag();
        }

        WalnutMainActivityAdapter.WalnutEventAppointmentMan man = (WalnutMainActivityAdapter.WalnutEventAppointmentMan)getItem(position);
        final int index = position;
        if(null != man.headPortrait && man.headPortrait.length() > 0){
            imageLoader.loadImage(man.headPortrait, viewHolder.bookerLogo, true, false);
        }
        viewHolder.bookerAlias.setText(man.userName);
        viewHolder.bookerStatus.setText(toChinese(man.appointmentStatus));
        viewHolder.bookerTime.setText(man.appointmentTime);
        viewHolder.bookerPhone.setText(man.phone);

        viewHolder.bookerPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phoneNumber = ((TextView)v).getText().toString();
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }
        });

        if ("apply".equals(man.appointmentStatus)) {
            viewHolder.bookerStatus.setVisibility(View.GONE);
            viewHolder.confirmView.setVisibility(View.VISIBLE);
            viewHolder.cancelView.setVisibility(View.VISIBLE);
            viewHolder.confirmView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    WalnutMainActivityAdapter.WalnutEventAppointmentMan item = (WalnutMainActivityAdapter.WalnutEventAppointmentMan)getItem(index);
                    WalnutMyServiceServiceHandler serviceServiceHandler = new WalnutMyServiceServiceHandler(context);
                    serviceServiceHandler.acceptBooking(RetrofitNetworkService.makeAsyncRestAdapter(), item.bookingId, notifyHandler, MeServiceDetailActivity.ACCEPT_BOOKING);
                }
            });
            viewHolder.cancelView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    WalnutMainActivityAdapter.WalnutEventAppointmentMan item = (WalnutMainActivityAdapter.WalnutEventAppointmentMan)getItem(index);
                    WalnutMyServiceServiceHandler serviceServiceHandler = new WalnutMyServiceServiceHandler(context);
                    serviceServiceHandler.cancelBooking(RetrofitNetworkService.makeAsyncRestAdapter(), item.bookingId, notifyHandler, MeServiceDetailActivity.CANCEL_BOOKING);
                }
            });
        } else {
            viewHolder.bookerStatus.setVisibility(View.VISIBLE);
            viewHolder.confirmView.setVisibility(View.GONE);
            viewHolder.cancelView.setVisibility(View.GONE);
            viewHolder.confirmView.setOnClickListener(null);
            viewHolder.cancelView.setOnClickListener(null);
        }

        return view;
    }

    public void notifyDataSetChanged(String bookingId, String state) {
        for (WalnutMainActivityAdapter.WalnutEventAppointmentMan item : dataset) {
            if (bookingId.equals(item.bookingId)) {
                item.appointmentStatus = state;
                break;
            }
        }

        notifyDataSetChanged();
    }

    private String toChinese(String state) {
        String cnState = "空";

        if ("apply".equals(state)) {
            cnState = "等待中";
        } else  if ("accepted".equals(state)) {
            cnState = "已同意";
        } else if ("rejected".equals(state)) {
            cnState = "已拒绝";
        } else if ("done".equals(state)) {
            cnState = "已完成";
        } else if ("cancel".equals(state)) {
            cnState = "已取消";
        }

        return cnState;
    }

    static class ViewHolder{
        @InjectView(R.id.item_appointment_hero_logo)ImageView bookerLogo;
        @InjectView(R.id.item_appointment_man_alias)TextView bookerAlias;
        @InjectView(R.id.item_appointment_status)TextView bookerStatus;
        @InjectView(R.id.item_appointment_phone)TextView bookerPhone;
        @InjectView(R.id.item_appointment_time)TextView bookerTime;
        @InjectView(R.id.item_appointment_confim)View confirmView;
        @InjectView(R.id.item_appointment_cancel)View cancelView;

        public ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }
}
